﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Globalization;
using DaphneTranslatorWPF.Mvvm;

namespace DaphneTranslatorWPF.ViewModel
{
    /// <summary>
    /// View model for culture selection dialog
    /// </summary>
    public class CultureSelectionViewModel
    {
        private List<string> m_lstCultures;
        private string m_sCultureFilter;

        public CultureSelectionViewModel()
        {
            Cultures = new ObservableCollection<string>();

            // initialize the culture list
            CultureInfo[] cultureInfoList = CultureInfo.GetCultures(CultureTypes.AllCultures);
            m_lstCultures = new List<string>();
            m_lstCultures.AddRange(cultureInfoList.Select(c => c.Name).ToList());

            BindCultureList();

            FilterTextChangedCommand = new DelegateCommand<object>(FilterTextChanged);
        }


        public string CultureFilter
        {
            get
            {
                return m_sCultureFilter;
            }
            set
            {
                m_sCultureFilter = value;
                BindCultureList();
            }
        }

        public string SelectedCulture
        { get; set; }

        public ObservableCollection<string> Cultures
        { get; set; }

        DelegateCommand<object> SelectCultureCommand;
        DelegateCommand<object> FilterTextChangedCommand;

        /// <summary>
        /// Filters out cultures
        /// </summary>
        protected void BindCultureList()
        {
            Cultures.Clear();

            if (string.IsNullOrWhiteSpace(CultureFilter))
            {
                foreach (var sCulture in m_lstCultures)
                    Cultures.Add(sCulture);
            }
            else
            {
                foreach (var sCulture in m_lstCultures)
                {
                    if (sCulture.Contains(CultureFilter))
                        Cultures.Add(sCulture);
                }
            }
        }

        protected void FilterTextChanged(object obj)
        {
            BindCultureList();
        }
    }
}
